package pl.edu.agh.mwo.importxls;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import data.Person;
import data.Project;
import data.Task;
import data.TaskList;


public class ImportExcel {

	public void readExcelFile(String filename, TaskList taskList) throws IOException {

		Person tempPerson = null;
		Project tempProject = null;
		boolean find = false;
		File newFile = new File(filename);
		String nameAndSurname = newFile.getName();
		
		String[] parts = nameAndSurname.split("\\."); // nazwisko_imie.xls
		if (parts.length < 2) return;
		if (!(parts[1].equals("xls"))) return;
		String[] parts2 = parts[0].split("_");
		String surname = parts2[0];
				
		find = false;
		for (Person p1: taskList.getPersonList()){
			if ((p1.getName().equals(parts2[1])) && (p1.getSurname().equals(surname))){
				find = true;
				tempPerson = p1;
			}
		}
		if (!find){
			tempPerson = new Person(parts2[1], surname);
			taskList.addPerson(tempPerson);
		}

		HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filename));

		String SheetProject, TaskName = "";
		long Day = 0;
		int Time = 0;
		 for (int k = 0; k < wb.getNumberOfSheets(); k++) {
	            HSSFSheet sheet = wb.getSheetAt(k); // select sheet
	            SheetProject = wb.getSheetName(k); // sheet name = project
	            
	            // find a project or add new
	    		find = false;
	    		for (Project pr1: taskList.getProjectList()){
	    			if (pr1.getName().equals(SheetProject)){
	    				find = true;
	    				tempProject = pr1;
	    			}
	    		}
	    		if (!find){
	    			tempProject = new Project(SheetProject);
	    			taskList.addProject(tempProject);
	    		}
	            
	            int rows = sheet.getPhysicalNumberOfRows(); // number of rows, 0 =
	                                                        // headline
	            for (int r = 1; r < rows; r++) {
	                boolean blad=false;
	                HSSFRow row = sheet.getRow(r);
	                if (row == null) {
	                    continue;
	                }
	               // int cells = row.getPhysicalNumberOfCells(); // columns
	                HSSFCell dateCell = row.getCell(0);
	                HSSFCell descriptionCell = row.getCell(1);
	                HSSFCell workloadCell = row.getCell(2);
	                if (dateCell==null || descriptionCell==null || workloadCell==null) {
	                	continue;
	                }
	                Day = (long) dateCell.getNumericCellValue();
	                TaskName = descriptionCell.getStringCellValue();
	                Time = (int) workloadCell.getNumericCellValue();
	                
					Date dateTask = new Date((Day - 25569) * 24 * 60 * 60 * 1000);
					Task t = new Task(Time, TaskName, dateTask, tempPerson,
							tempProject);
					if (!taskList.getTaskList().contains(t)) {
						taskList.addTask(t);
					}
	            }
	        }
	}
}
